﻿using Ekton_Manage_DomainCore.Aggregate;
using Ekton_Manage_DTO.RequestModels.Product;
using Ekton_Manage_Framework.Methods;
using Ekton_Manage_Framework.Redis;
using Ekton_Manage_IBusiness;
using Ekton_Manage_InfrastructureCore.Common;
using Ekton_Manage_InfrastructureCore.Repositories;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Ekton_Manage_Business
{
    public class APIProduct : IApiSelectProduct
    {

        private readonly IBaseRepository<product, int> _basepProductRepository;
        private readonly IBaseRepository<product_type, int> _baseProductTypeRepository;
        private readonly IBaseRepository<material, int> _baseMaterialsRepository;


        public APIProduct(IBaseRepository<product_type, int> baseProductTypeRepository, IBaseRepository<product, int> baseProductRepository, IBaseRepository<material, int> baseMaterialsRepository)
        {

            _basepProductRepository = baseProductRepository;
            _baseProductTypeRepository = baseProductTypeRepository;
            _baseMaterialsRepository = baseMaterialsRepository;
        }

        /// <summary>
        ///  查询全部产品
        /// </summary>
        public async Task<Message> ApiGetProductList()
        {
            var list = new List<product_type>();
            //查询产品类型表
            var productType = await _baseProductTypeRepository.QueryTableList(m => true);
            #region 判断如果该产品分类下没有产品 则不显示该产品分类
            foreach (var item in productType)
            {
                var count = await _basepProductRepository.QuerySingleData(m => m.classify_id == item.id);
                if (count is null) continue;
                list.Add(new product_type
                {
                    id = item.id,
                    product_picture = item.product_picture,
                    class_name = item.class_name,
                    rank = item.rank,
                    father_name = item.father_name,
                    father_id = item.father_id,
                    product_describe = item.product_describe,
                    product_id = item.product_id
                });
            }
            #endregion
            if (list.Count == 0) return new Message(false, 202);
            //取产品类型索引为0的那一项作为返回值
            var product = await _basepProductRepository.QueryTableList(m => m.classify_id == list[0].id);
            var materials = new material();
            //根据索引为0的产品的materials_id查询材料信息
            if (product.Count > 0)
                materials = await _baseMaterialsRepository.QuerySingleData(product[0].materials_id);
            DtoProductResponse result = new();
            list.ForEach((m) =>
            {
                result.productTypes.Add(new ProductType
                {
                    id = m.id,
                    productTypeName = m.class_name,
                    productTypeUrl = m.product_picture,
                    rank = m.rank
                });
            });

            product.ForEach((m) =>
            {
                result.dtoProductArrays.Add(new DtoProductArray
                {
                    id = m.id,
                    productName = m.product_name,
                    productUrl = m.product_picture,
                    design = m.design,
                    product_describe = m.product_describe,
                    materialsName = m.materialItem
                });
            });

            return new Message(result);
        }

        /// <summary>
        /// 选择产品/材料传入商品id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Message> ApiIdGetProductList(int id)
        {
            //根据id查询产品类型表
            var productType = await _baseProductTypeRepository.QuerySingleData(m => m.id == id); if (productType == null) return new Message(false, 202);
            //根据查出来的产品类型信息再来查询商品信息表
            var product = await _basepProductRepository.QueryTableList(m => m.classify_id == productType.id);
            var materials = new material();

            if (product.Count > 0)
                materials = await _baseMaterialsRepository.QuerySingleData(product[0].materials_id);
            DtoProductResponse result = new();
            product.ForEach((m) =>
            {
                result.dtoProductArrays.Add(new DtoProductArray
                {
                    id = m.id,
                    productName = m.product_name,
                    productUrl = m.product_picture,
                    design = m.design,
                    product_describe = m.product_describe,
                    materialsName = m.materialItem,
                });
            });
            return new Message(result);
        }

        /// <summary>
        /// 选择设计规格
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Message> ApiIdGetdesign(int id)
        {
            var designs = await _basepProductRepository.QuerySingleData(m => m.id == id); if (designs == null) return new Message(false, 202);

            DtoProductResponse result = new();
            result.design.Add(new DtoDesign
            {
                id = designs.id,
                design = designs.design
            });
            return new Message(result);
        }
    }

}
